package com.hank.swagger.service.doc.config;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * @author hank
 * @since 2021/7/5 0005 上午 11:27
 */
@Component("appApiConfig")
@Slf4j
@AllArgsConstructor
@NoArgsConstructor
@Builder
@RefreshScope
@Data
public class AppApiConfig {

    /**
     * app的配置信息
     * wjj-push-core:推送服务,service-order:订单服务
     */
    private String appNameMapStr;

    /**
     * 首页文档
     */
    @Value("${app-api-config.apiIndexDoc:}")
    private String apiIndexDoc = "";

    private Map<String, String> appNameMap = Maps.newLinkedHashMap();


    @PostConstruct
    public void initialize() {
        log.info("appApiConfig: {}", this);
    }

    @Value("${app-api-config.appNameMap:}")
    public void setAppNameMapStr(String appNameMapStr) {
        this.appNameMapStr = appNameMapStr;
        if (StrUtil.isNotBlank(appNameMapStr)) {
            String[] appNameStrArr = appNameMapStr.split(",");
            for (String appNameStr : appNameStrArr) {
                String[] appNameArr = appNameStr.split(":");
                appNameMap.put(appNameArr[0], appNameArr.length > 1 ? appNameArr[1] : "");
            }
        }
    }

    public Map<String, String> getAppNameMap() {
        return appNameMap;
    }
}
